<?php

namespace app\storeapi\model\store;

use think\Model;
use think\Request;

/**
 * 商家用户模型
 * Class StoreUser
 * @package app\store\model
 */
class User extends Model
{
    protected $name = 'store_user';
    protected $hidden = ['password','create_time','update_time'];
    /**
     * 商家用户登录
     * @param $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login($data)
    {

        // 验证用户名密码是否正确
//        if (!$user = $this->getLoginUser($data['username'], $data['password'])) {
//            $this->error = '登录失败, 用户名或密码错误';
//            return false;
//        }
//        if (empty($user['wxapp'])) {
//            $this->error = '登录失败, 未找到小程序信息';
//            return false;
//        }
//        if ($user['wxapp']['is_recycle']) {
//            $this->error = '登录失败, 当前小程序商城已删除';
//            return false;
//        }
//        if (!$user['wxapp']['active']) {
//            $this->error = '登录失败, 当前小程序未激活';
//            return false;
//        }
//        $data = ['wxapp_id'=>$user['wxapp']['wxapp_id'],'login_user_id'=>$user['store_user_id'],'identity'=>1];
//        return $data;


        $user=self::useGlobalScope(false)->with(['wxapp'])->where(['user_name' =>$data['username'],'is_delete' => 0])->find();
        if(!$user){
            $this->error = '登录失败, 用户名不存在';
            return false;
        }else{
            //$pass= md5(md5($data['password']).$user['salt'].config('salt'));
            $pass= md5(md5($data['password'].$user['salt'].config('salt')));
            if($pass==$user['password']){
                if (empty($user['wxapp'])) {
                    $this->error = '登录失败, 未找到小程序信息';
                    return false;
                }
                if ($user['wxapp']['is_recycle']) {
                    $this->error = '登录失败, 当前小程序商城已删除';
                    return false;
                }
                if($user['wxapp']['active']==0){
                    $this->error = '账号尚未激活，请联系管理员';
                    return false;
                }
                // 保存登录状态
                $data = ['wxapp_id'=>$user['wxapp']['wxapp_id'],'login_user_id'=>$user['store_user_id'],'identity'=>1];
                return $data;
            }else{
                $this->error = '登录失败, 用户名或密码错误';
                return false;
            }
        }


    }


    /**
     * 商家用户检测
     * @param $data
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkState($data)
    {
        $user = $this->where(['store_user_id'=>$data])->find();

        if($user['is_delete']==1){
            $this->error = '该账号已被删除';
            return false;
        }
        return true;
    }


    /**
     * 获取登录用户信息
     * @param $user_name
     * @param $password
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getLoginUser($user_name, $password)
    {
        return (new static())->with(['wxapp'])->where([
            'user_name' => $user_name,
            'password' => yoshop_hash($password),
            'is_delete' => 0
        ])->find();
    }

    /**
     * 关联微信小程序表
     * @return \think\model\relation\BelongsTo
     */
    public function wxapp()
    {
        return $this->belongsTo("app\\storeapi\\model\\Wxapp")->field('wxapp_id,is_recycle,active');
    }


    /**
     * 忘记密码
     * @param $param
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function forgetPass($param)
    {
        //验证密码
        if (!$this->checkPass($param['newpassword'], $param['againpassword'])) {
            $this->error = '两次密码不一致';
            return false;
        }
        // 检测账号
        if (!$user = $this->checkUser($param['user_name'])) {
            $this->error = '账号错误';
            return false;
        }

        $user = self::useGlobalScope(false)->with(['wxapp'])->where(['user_name' =>$param['user_name'],'is_delete' => 0])->find();
        $password = md5(md5($param['newpassword'].$user['salt'].config('salt')));

        //$password = yoshop_hash($param['newpassword']);
        return $this->isUpdate(true,['user_name'=>$param['user_name']])->save(['password'=>$password]);

    }

    /**
     * 检测账号
     * @param $user_name
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function checkUser($user_name)
    {
        return (new static())->where(['user_name'=>$user_name])->find();
    }


    /**
     * 检测密码
     * @param $newpassword
     * @param $againpassword
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function checkPass($newpassword, $againpassword)
    {
        if($newpassword!=$againpassword) return false;
        return true;
    }






}
